/**
 * Created by zhang on 2016/12/14.
 */
var visitorOrder = {
    events:{
        "#visitorOrderBody .order-btn click" : "visitorOrder",
        "#visitorOrderBody .search-btn click" : "searchList",
        "#visitorOrderBody .revoke-visit click" : "revokeVisit"
    },
    init: function () {
        var _this = this;
        document.title = "访客预约 - 访客管理";
        var url = interfaceURL.getDepartmentList(),
            depart = _this.getList(null,url);
        var temp = Util.template($("#visitorOrder-template").html(),{depart:depart});
        $(".commonBody-rightBody").html(temp);
        Util.event.call(_this,_this.events);
        _this.initTable();
    },
    getList : function(para,url){
        var result = null;
        Util.ajaxRequest({
            type: "get",
            url: url,
            dataType: "json",
            showLoading : false,
            async : false,
            data:para,
            timeout:60000,
            success : function(data){
                if(data.code == 200){
                    result = data.result;
                }
            }
        })
        return result;
    },
    datetime : function(){
        var d = new Date(),
            vYear = d.getFullYear(),
            vMon = d.getMonth() + 1,
            vDay = d.getDate(),
            h = d.getHours(),
            m = d.getMinutes();
        $(".date-from").datetimepicker({
            format: 'yyyy-mm-dd hh:ii',
            autoclose: true,
            language: "zh-CN",
            startDate:""+vYear+"-"+vMon+"-"+vDay+" "+h+":"+m+"",
            pickerPosition: "bottom-left"
        });
    },
    searchList : function(){
        this.initTable(1);
    },
    initTable: function (page) {
        var _this = this,
            depId = $("#depart").val(),
            department = $("#department").val(),
            inviterName = $("#inviterName").val(),
            name = $("#name").val(),
            state = $("#state").val();
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.getVisitorOrderList(),
            data:{
                depId : depId,
                department : department,
                inviterName : inviterName,
                name : name,
                state : state,
                pageNum:Util.isEmpty(page)?1:page,
                pageSize:pageInfo.pageSize()
            },
            showLoading : false,
            async:false,
            timeout:60000,
            success : function(data){
                if(data.code == 200){
                    $.each(data.result.results, function (i, n) {
                        if (n.state == "0") {
                            n.caozuo = "<button  class='btn btn-blue revoke-visit'>撤销</button>";
                        }else{
                            n.caozuo = "<button  class='btn btn-gray' disabled>撤销</button>";
                        }
                        n.visiteTime =  new Date(n.planStartTime).format("yyyy-MM-dd HH:mm") +"--"+ new Date(n.planEndTime).format("yyyy-MM-dd HH:mm");
                    });
                    $(".list-box").cakeTable({
                        head: [   
                            {field: "name",name: "来访姓名"},
                            {field: "sex",name: "性别",rule:{"1":"男","0":"女"}},
                            {field: "idCard",name: "身份证号码"},
                            {field: "department",name:"所属单位"},
                            {field: "inviterName",name: "受访人员"},
                            {field: "createName",name: "提交人员"},
                            {field: "visiteTime",name:"来访时间",type:2},
                            {field: "reason",name:"事由"},
                            {field: "state",name:"状态",rule:{"0":"已预约","1":"访问中","2":"访问结束","3":"已撤销"}},
                            {field: "caozuo",name:"操作",type:2}
                        ],
                        rowData:[
                            {key:"id",attrName:"id"}
                        ],
                        columns:data.result.results,
                        sort:false,
                        showColumns :false,
                        pageInfo:data.result.pagination2,
                        align:"center",
                        pageType:"out",
                        page : function(page){
                            _this.initTable(page);
                        },
                        done:function(){}
                    })
                }   
            }
        });
    },
    visitorOrder : function(){
        var _this = this,
            userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
        pop.dialog({
            popTitle: "来访人员预约",
            popContent: Util.template($("#order-pop-template").html(), {}),
            width :　"630px",
            popOkText : "提交",
            onLoad: function () {
                _this.datetime();
                $("#departs").val(userInfo.depName);
                $("#users").val(userInfo.roleName);
            }
        },function(){
            var idCard = $("#v-idCard").val(),
                department = $("#v-department").val(),
                name = $("#v-name").val(),
                planEnd = $("#v-planEnd").val(),
                planStart = $("#v-planStart").val(),
                reason = $("#v-visiteReason").val(),
                sex = $("#v-sex").val(),
                tel = $("#v-tel").val();
            var flag = true;
            if (!Util.isEmpty(idCard,department,name,planEnd,planStart,reason,sex,tel)) {
                Util.ajaxRequest({
                    type: "get",
                    url: interfaceURL.addVisitOne(),
                    dataType: "json",
                    showLoading : false,
                    async : false,
                    autoError:false,
                    timeout:60000,
                    data:{
                        idCard:idCard,
                        department:department,
                        name:name,
                        planEnd:planEnd,
                        planStart:planStart,
                        reason:reason,
                        sex : sex,
                        tel:tel,
                        depId:userInfo.depId,
                        inviter:userInfo.userId,
                        inviterName : userInfo.realName
                    },
                    success : function(data){
                        if(data.code == 200){
                            pop.alert("登记成功！",function(){
                                _this.initTable()
                                flag = true;
                            })
                        }else{
                            pop.alert(data.message);
                            flag = false;
                        }
                    }
                })
                return flag
            }else{
                pop.alert("请填将内容填写完整！")
                return false
            }
        })
    },
    revokeVisit : function(ele){
        var _this = this,
            id = $(ele).parents("tr").attr("id"),
            url = interfaceURL.visitOne(),
            detail = _this.getList({id:id},url);
        pop.dialog({
            popTitle: "撤销",
            popContent: Util.template($("#revoke-pop-template").html(), {detail:detail}),
            width :　"500px",
            popOkText : "撤销",
            popCancelText : "返回"
        },function(){
            var undoReason = $("#revokeReason").val();
            if (!Util.isEmpty(undoReason)) {
                Util.ajaxRequest({
                    type: "get",
                    url: interfaceURL.revokeVisit(),
                    dataType: "json",
                    showLoading : false,
                    async : false,
                    timeout:60000,
                    data:{
                        id:id,
                        undoReason : undoReason
                    },
                    success : function(data){
                        if(data.code == 200){
                            pop.alert("撤销成功！",function(){
                                _this.initTable()
                            })
                        }
                    }
                })
            }else{
                pop.alert("请填写撤销原因！")
                return false
            }
        })
    }
};